﻿Imports Dainfo.ControleEstoque.Dados
Imports Dainfo.ControleEstoque.Model

Partial Public Class PesquisaFornecedor
    Inherits System.Web.UI.Page

    Private Const keyFornecedores As String = "Fornecedores"
    Private _dadosFornecedor As New DadosFornecedor()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            GetAllDataFromDB()
            BindData()
        End If

    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvFornecedor.RowEditing
        gvFornecedor.EditIndex = e.NewEditIndex
        BindData()
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvFornecedor.RowUpdating
        'Retrieve the table from the session object.
        Dim clientes As New List(Of Fornecedor)()
        clientes = CType(ViewState(keyFornecedores), List(Of Fornecedor))

        'Update the values.
        Dim row As GridViewRow = gvFornecedor.Rows(e.RowIndex)
        Dim cliente As Fornecedor = clientes(row.DataItemIndex)

        cliente.RazaoSocial = CType(row.Cells(2).Controls(0), TextBox).Text
        cliente.NomeContato = CType(row.Cells(3).Controls(0), TextBox).Text
        cliente.Produtos = CType(row.Cells(4).Controls(0), TextBox).Text
        cliente.Codigo = CType(row.Cells(5).Controls(0), TextBox).Text
        cliente.CPF_CNPJ = CType(row.Cells(6).Controls(0), TextBox).Text
        cliente.Estado = CType(row.Cells(7).Controls(0), TextBox).Text
        cliente.Cidade = CType(row.Cells(8).Controls(0), TextBox).Text
        cliente.Bairro = CType(row.Cells(9).Controls(0), TextBox).Text
        cliente.Endereco = CType(row.Cells(10).Controls(0), TextBox).Text
        cliente.CEP = CType(row.Cells(11).Controls(0), TextBox).Text
        cliente.Telefone1 = CType(row.Cells(12).Controls(0), TextBox).Text
        cliente.Telefone2 = CType(row.Cells(13).Controls(0), TextBox).Text
        cliente.Email = CType(row.Cells(14).Controls(0), TextBox).Text
        cliente.Messenger = CType(row.Cells(15).Controls(0), TextBox).Text
        cliente.Observacoes = CType(row.Cells(16).Controls(0), TextBox).Text

        'Reset the edit index.
        gvFornecedor.EditIndex = -1

        _dadosFornecedor.Update(cliente)
        GetAllDataFromDB()

        'Bind data to the GridView control.
        BindData()
    End Sub

    Private Sub BindData()
        gvFornecedor.DataSource = ViewState(keyFornecedores)
        gvFornecedor.DataBind()
    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPesquisar.Click
        GetFilteredDataFromDB()
        BindData()
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvFornecedor.RowCancelingEdit
        'Reset the edit index.
        gvFornecedor.EditIndex = -1
        'Bind data to the GridView control.
        BindData()
    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvFornecedor.PageIndexChanging
        gvFornecedor.PageIndex = e.NewPageIndex
        'Bind data to the GridView control.
        BindData()
    End Sub


    Sub GetAllDataFromDB()
        ViewState(keyFornecedores) = _dadosFornecedor.GetAll()
    End Sub

    Sub GetFilteredDataFromDB()

        If txtCodigo.Text <> "" Then
            Dim c As Fornecedor = _dadosFornecedor.GetById(txtCodigo.Text)
            Dim clientes As New List(Of Fornecedor)()
            If Not c Is Nothing Then
                clientes.Add(c)
                ViewState(keyFornecedores) = clientes
            End If
        Else

            ViewState(keyFornecedores) = _dadosFornecedor.GetByNome(txtNome.Text)
        End If
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvFornecedor.RowDeleting
        'Retrieve the table from the session object.
        Dim clientes As New List(Of Fornecedor)()
        clientes = CType(ViewState(keyFornecedores), List(Of Fornecedor))

        'Delete the value.
        Dim row As GridViewRow = gvFornecedor.Rows(e.RowIndex)
        Dim cliente As Fornecedor = clientes(row.DataItemIndex)
        _dadosFornecedor.Delete(cliente.Codigo)

        'BindGrid
        GetAllDataFromDB()
        BindData()
    End Sub

End Class